<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">所属农场</label>
        <el-select v-model="query.farm_id" filterable placeholder="请选择" style="width: 185px;" class="filter-item">
          <el-option v-for="item in dict.farms" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="600px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="农场" >
            <el-input v-model="form.name" readonly style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea"
                      :rows="5" style="width: 370px;" />
          </el-form-item>

          <el-form-item label="logo">
            <el-upload
                ref="upload"
                list-type="picture-card"
                class="upload-demo"
                action="#"
                :file-list="fileList"
                :auto-upload="false"
                :limit=1
            >
              <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
              <div slot="tip" class="el-upload__tip">只能上传图片文件，且不超过512kb</div>
            </el-upload>

          </el-form-item>


        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="所属农场" width="200" />
        <el-table-column prop="remark" label="农场介绍" width="500" />
        <el-table-column
          v-if="checkPer(['admin', 'farmBase:edit', 'farmBase:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" :show-dle="false"/>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
/** 耕整地履历 */
import crudFieldFarmBase from '@/api/jf/farmBase'
import { getFieldListByFarm } from '@/api/jf/public'
import CRUD, { crud, form, header, presenter } from '@crud/V2/crud'
import crudOperation from '@crud/V2/CRUD.operation'
import pagination from '@crud/V2/Pagination'
import rrOperation from '@crud/V2/RR.operation'
import udOperation from '@crud/V2/UD.operation'
import {mapGetters} from "vuex";

const defaultForm = {
  id: null,
  remark: null,
  logo_image: null
}
export default {
  name: 'FarmBase',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '农场基本信息',
      url: 'api/v2/base/farm/farmBase',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: { ...crudFieldFarmBase }
    })
  },
  dictsV2: ['farms'],
  data() {
    return {
      fileList: [{ id: '001', name: 'food.jpeg', url: '/api/v2/public/getImage?id=11' }],
      permission: {
        add: ['admin', 'farmBase:add'],
        edit: ['admin', 'farmBase:edit'],
        del: ['admin', 'farmBase:del']
      },
      rules: {
        farm_id: [{ required: true, message: '不能为空', trigger: 'blur' }],
      },
      queryTypeOptions: [
        { key: 'farm_id', display_name: '农场编号' },
      ],
      fields: []
    }
  },
  created() {
    this.crud.optShow.add = false
    this.crud.optShow.del = false
    this.crud.optShow.download = false
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.beforeToEdit](crud, form) {
    },

    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      // console.log(form.images)
      this.fileList = []
      if (form.logo_image != null) {
        this.fileList.push({
          id: form.logo_image,
          name: form.logo_image,
          url: this.baseApi + '/api/v2/public/getImage?id=' + form.logo_image
        })
      }
    },
    [CRUD.HOOK.beforeValidateCU](crud) {
      // 检查并整理图片数据
      crud.form.oldImages = []
      crud.form.newImages = []
      var files = this.$refs.upload.uploadFiles
      for (var i = 0; i < files.length; i++) {
        var file = files[i]
        if (files[i].hasOwnProperty('raw')) {
          var imglist = ['image/png', 'image/jpg', 'image/jpeg', 'image/bmp', 'image/gif']
          var isImage = imglist.some(function(item) {
            return item === file.raw.type
          })
          const isLt2M = file.raw.size / 1024 / 1024 < 0.5
          if (!isImage) {
            crud.notify('上传图片只能是图片格式!', CRUD.NOTIFICATION_TYPE.ERROR)
            return false
          }
          if (!isLt2M) {
            crud.notify('上传图片大小不能超过 512KB!', CRUD.NOTIFICATION_TYPE.ERROR)
            return false
          }
          crud.form.newImages.push(file.raw)
        } else {
          crud.form.oldImages.push(file.id)
        }
      }
      // magic-api不支持数组参数，转为字符
      // crud.form.oldImages = crud.form.oldImages.toString()
      return true
    },
    [CRUD.HOOK.afterSubmit](crud) {
      this.fileList = []
    },
    [CRUD.HOOK.beforeAddCancel](crud) {
      this.fileList = []
    },
    [CRUD.HOOK.beforeEditCancel](crud) {
      this.fileList = []
    },

    // 获取田块
    changeFram(value) {
    }
  }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
